FriendsPortlet = Class.extend({
	init: function()	{
		this.name = "FriendsPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onReloadPage: function()	{
		this.run();
	},
	
	onInviteButtonClick: function()	{
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('friend', 'add-friend', {id: id}, 'POST', {
			'onSuccess': function(ret)	{
				obj.onReloadPage();
			}
		});
	},
	
	onAcceptPendingClick: function()	{
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('friend', 'accept-request', {id: id}, 'POST', {
			'onSuccess': function(ret)	{
				obj.onReloadPage();
			}
		});
	},
	
	run: function()	{
		var id = this.getRequest().getParam('id');
		var props = SingletonFactory.getInstance(Application).getSystemProperties();
		var loggedin = props.get('user.login');
		if (loggedin == 0)
			return;
		var uid = props.get('user.id');
		if (uid == id)
			return;
		var obj = this;
		this.getPortletPlaceholder().paintCanvas(this.render());
		this.onAjax('friend', 'is-in-friend', {id: id}, 'GET', {
			'onSuccess': function(ret)	{
				if (ret == false)	{
					obj.requestForEffectiveResource('Main').html(obj.renderView('InviteTmpl', {}));
				} else {
					if (ret.state == 1)
						return;
					if (ret.user_id2 == id)	{
						obj.requestForEffectiveResource('Main').html(obj.renderView('PendingTmpl', {}));
					} else {
						obj.requestForEffectiveResource('Main').html(obj.renderView('AcceptTmpl', {}));
					}
				}
			}
		});
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);